package com.yang.scheduler;

import com.yang.constant.ConfiConstant;
import com.yang.dao.TimeSchedulerDao;
import com.yang.entity.OrderFormEntity;
import com.yang.entity.RelationEntity;
import com.yang.entity.RoomEntity;
import org.jboss.logging.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Lazy(value=false)
public class TimeSchedulerServiceImpl{
    private static Logger logger = Logger.getLogger(TimeSchedulerServiceImpl.class);

    @Resource
    private TimeSchedulerDao timeSchedulerDao;

    @Scheduled(cron="0 0 0 * * ? ")//每天零点触发
//   @Scheduled(cron="0/5 * *  * * ? ")
    public void modificationPastOrderScheduler() {
        List<RelationEntity> relationEntities = timeSchedulerDao.selectRelationByOrderId(ConfiConstant.UNHISTORICAL_RELATIONSHIP);
        for(RelationEntity relationEntity : relationEntities){
            OrderFormEntity orderFormEntity = relationEntity.getOrderFormId();
            long today = System.currentTimeMillis();
            if(today < (orderFormEntity.getStayDay()*ConfiConstant.DAY+orderFormEntity.getBeginDay())){
                continue;
            }
            relationEntity.setStatus(ConfiConstant.HISTORICAL_RELATIONSHIP);
            RoomEntity roomEntity = relationEntity.getRoomId();
            roomEntity.setStatus(ConfiConstant.EMPTY_ROOM);
            timeSchedulerDao.updateRoom(roomEntity);
            timeSchedulerDao.updateRelation(relationEntity);

        }
        logger.info("定时器");
    }
}
